package com.ciquan.server.controller;

import com.ciquan.server.bean.PraiseRecord;
import com.ciquan.server.bean.UserInfo;
import com.ciquan.server.service.ArtistInfoService;
import com.ciquan.server.service.PraiseRecordService;
import com.ciquan.server.service.UserInfoService;
import com.ciquan.server.service.WorksInfoService;
import com.ciquan.server.util.web.ParameterMap;
import com.ciquan.server.util.web.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by card on 2015/8/30.
 */
@Controller
@RequestMapping("/api/praise/")
public class ServicePraiseRecordController extends AbstractController {

    @Resource
    private PraiseRecordService praiseRecordService;

    @Resource
    private WorksInfoService worksInfoService;

    @Resource
    private ArtistInfoService artistInfoService;

    @Resource
    private UserInfoService userInfoService;

    @RequestMapping(value = "/praise", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String praise(HttpServletRequest request) {
        Result result = new Result();
        result.setSuccess(false);
        if(!isLogin(request)) {
            result.setStatusmsg("用户未登录");
        }

        else {
            try {
                int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
                int worksId = ServletRequestUtils.getIntParameter(request, "worksId", 0);
                int type = ServletRequestUtils.getIntParameter(request, "type", 0);
                PraiseRecord praiseRecord = new PraiseRecord();
                praiseRecord.setUserId(userId);
                praiseRecord.setWorksId(worksId);
                praiseRecord.setType(type);
                praiseRecordService.insert(praiseRecord);
                result.setSuccess(true);
                result.setStatusmsg("点赞成功");
            } catch (Exception e) {
                logger.error(e);
                result.setStatusmsg("您已经点过赞了");
            }
        }
        return result.toString();
    }

    @RequestMapping(value = "/cancel", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String cancel(HttpServletRequest request) {
        Result result = new Result();
        result.setSuccess(false);
        if(!isLogin(request)) {
            result.setStatusmsg("用户未登录");
        }

        else {
            try {
                int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
                int worksId = ServletRequestUtils.getIntParameter(request, "worksId", 0);
                PraiseRecord praiseRecord = new PraiseRecord();
                praiseRecord.setUserId(userId);
                praiseRecord.setWorksId(worksId);
                praiseRecordService.delete(praiseRecord);
                result.setSuccess(true);
                result.setStatusmsg("取消成功");
            } catch (Exception e) {
                logger.error(e);
                result.setStatusmsg("您已经取消点赞");
            }
        }
        return result.toString();
    }

    @RequestMapping(value = "/list", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String list(HttpServletRequest request) {
        Result result = new Result();
        result.setSuccess(false);
        try {
            int worksId = ServletRequestUtils.getIntParameter(request, "worksId", 0);
            int page = ServletRequestUtils.getIntParameter(request, "page", 1);
            int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 30);
            int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
            ParameterMap parameterMap = new ParameterMap();
            parameterMap.setStart((page - 1) * pageNum);
            parameterMap.setPageNum(pageNum);
            parameterMap.put("worksId", worksId);
            parameterMap.put("type", 0);
            List<UserInfo> userInfos = praiseRecordService.selectList(parameterMap);
            int count = praiseRecordService.selectCount(parameterMap);

            if(userId != 0) {
                parameterMap.put("userId", userId);
                int praise = praiseRecordService.selectCount(parameterMap);
                result.getMap().put("praise", praise);
            }

            result.setSuccess(true);
            result.setStatusmsg("获取列表成功");
            result.setData(userInfos);
            result.setTotal(count);
        } catch (Exception e) {
            logger.error(e);
            result.setStatusmsg("服务器错误");
        }
        return result.toString();
    }

    @RequestMapping(value = "/tips", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String tips() {
        Result result = new Result();
        result.setSuccess(true);
        List<PraiseRecord> praiseRecords = praiseRecordService.selectTop();
        wrapPraiseRecords(praiseRecords);
        result.setData(praiseRecords);
        result.setStatusmsg("获取列表成功");
        return result.toString();
    }

    @RequestMapping(value = "/praiseList", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String praiseList(HttpServletRequest request) {
        Result result = new Result();
        int page = ServletRequestUtils.getIntParameter(request, "page", 1);
        int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 30);

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.setStart((page - 1) * pageNum);
        parameterMap.setPageNum(pageNum);

        List<PraiseRecord> praiseRecords = praiseRecordService.praiseList(parameterMap);
        wrapPraiseRecords(praiseRecords);
        result.setSuccess(true);
        result.setStatusmsg("获取列表成功");
        result.setData(praiseRecords);
        return result.toString();
    }

    private void wrapPraiseRecords(List<PraiseRecord> praiseRecords){
        for (PraiseRecord praiseRecord : praiseRecords) {

            if(praiseRecord.getType() == 0) {
                praiseRecord.setUserName(userInfoService.selectById(praiseRecord.getUserId()).getName());
                praiseRecord.setWorksName(worksInfoService.selectById(praiseRecord.getWorksId()).getName());
            }

            else if(praiseRecord.getType() == 1) {
                praiseRecord.setUserName(userInfoService.selectById(praiseRecord.getUserId()).getName());
                praiseRecord.setWorksName(worksInfoService.selectById(praiseRecord.getWorksId()).getName());
            }

            else if(praiseRecord.getType() == 2) {
                praiseRecord.setUserName(userInfoService.selectById(praiseRecord.getUserId()).getName());
                praiseRecord.setWorksName(artistInfoService.selectById(praiseRecord.getWorksId()).getName());
            }

            else if(praiseRecord.getType() == 3) {
                praiseRecord.setUserName(userInfoService.selectById(praiseRecord.getUserId()).getName());
                praiseRecord.setWorksName(userInfoService.selectById(praiseRecord.getWorksId()).getName());
            }
        }
    }

}
